package com.team.arms.xitong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.arms.common.Result;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.utils.PageUtil;
import com.team.arms.xitong.entity.DataMamage;
import com.team.arms.xitong.entity.dto.DataDto;
import com.team.arms.xitong.entity.vo.DataVo;
import com.team.arms.xitong.mapper.DataMamageMapper;
import com.team.arms.xitong.service.IDataMamageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huazige
 * @since 2022-06-25
 */
@RestController
@RequestMapping("/dataMamage")
@CrossOrigin
@Api(tags = "系统管理-数据管理")
public class DataMamageController {

    @Autowired
    DataMamageMapper dataMamageMapper;
    @Autowired
    IDataMamageService iDataMamageService;
    @Autowired
    private ConvertUtil convertUtil;

    @ApiOperation("分页显示所有数据信息")
    @GetMapping(value = "/list/{pageNum}/{pageSize}")
    public Result selectMsg(
            @ApiParam("当前页数")       @PathVariable("pageNum")Integer pageNum,
            @ApiParam("每页的数据量")       @PathVariable("pageSize")Integer pageSize

    ){
        List<DataMamage> dataMamages = dataMamageMapper.selectList(null);

//        List<UserVo>  userVoList=  convertUtil.convert(dicTypes, UserVo.class);

        //转换效率的问题,直接sql查询返回这个类型效率相对高一些
        Page page = PageUtil.list2Page(dataMamages, pageNum, pageSize);
        return page !=null ? Result.success("分页成功").data(page): Result.error("分页失败");
    }

    @ApiOperation("修改数据状态信息")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody DataVo dataVo){
        String dataId = dataVo.getDataId();
        QueryWrapper<DataMamage> wrapper = new QueryWrapper();
        wrapper.eq("data_id",dataId);
        DataMamage convert = convertUtil.convert(dataVo, DataMamage.class);
        boolean b =   iDataMamageService.update(convert,wrapper);//前新后旧

        return b ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }
    @ApiOperation("单个+批量删除数据信息")
    @DeleteMapping("/deletelist")
    public Result deleteInformation(
            @ApiParam("数据表主键") @RequestParam("dataId")List<String> deletelist ) {


            boolean b = iDataMamageService.removeByIds(deletelist);
        return b ? Result.success("批量删除成功") : Result.error("批量删除失败");
        }
    @ApiOperation("新增数据信息")
    @PostMapping("/inster")
    @Transactional
    public Result install(@RequestBody DataDto dataDto){

        DataMamage convert = convertUtil.convert(dataDto, DataMamage.class);
        dataMamageMapper.insert(convert);
        return Result.success("添加成功").data(convert) ;

    }

    }
